package com.example.spring3.controller;

import com.example.spring3.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
//@Controller
@RestController
public class UserController {
    @Autowired
    UserService userService;
    @PostMapping(value = "/register/{username}/{password}/{phone}/{email}")
    public Object addUser(
            @PathVariable("username") String username,
            @PathVariable("password") String password,
            @PathVariable("phone") String phone,
            @PathVariable("email") String email
    ) {
        if(StringUtils.hasText(username) && StringUtils.hasText(password)){
            if(userService.slectByUsername(username)==0){
                userService.insertUser(username,password,phone,email);
                return userService.getUserByUsername(username);
            }
            else {
                String error="用户名已存在";
                Map<String,Object> map=new HashMap<>();
                map.put("error",error);
                return map;
            }
        }
        else {
            String error="用户名或密码不能为空";
            Map<String,Object> map=new HashMap<>();
            map.put("error",error);
            return map;
        }
    }
//    @RequestMapping(value = "/user_register")
//    public String register(){
//        return "register";
//    }
//    @Autowired
//    UserService userService;
//    @RequestMapping(value = "/register")
//    public String register(String username,String password,String phone,String email){
//        if(StringUtils.hasText(username) && StringUtils.hasText(password)){
//            if(userService.slectByUsername(username)==0){
//                userService.insertUser(username,password,phone,email);
//                return "register_success";
//            }
//            else{
//                return "register_fail";
//            }
//        }
//        else {
//            return "register_error";
//        }
//    }
//    @GetMapping(value = "/user_register")
//    public String register(){
//        return "register";
//    }

}
